# -*- coding: utf-8 -*-
# Created by 'Zhou Bingbing'  on 2019/4/19
import execjs
import requests
class Baidu():
    headers = {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Content-Length': '157',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Cookie': 'BAIDUID=959BD58239197A4C8C27D9AA6CE6802A:FG=1; BIDUPSID=959BD58239197A4C8C27D9AA6CE6802A; PSTM=1537491023; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; BDUSS=m5CZ3VNYWIxZWhzWkh6U2RTQ3Rockt0Z282U2huQTN-OXhkajZoRmhCYzFrTk5jSVFBQUFBJCQAAAAAAAAAAAEAAABW3BHTaHN6YjIyNTgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADUDrFw1A6xcY; DOUBLE_LANG_SWITCH=0; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; locale=zh; BCLID=11657211867648520518; BDSFRCVID=yIAOJeC62ABh8s59xJai2QKtlcL8MqvTH6aok6PAgEX5-VBnyVrgEG0Pex8g0KAbFpPcogKK0eOTHktF_2uxOjjg8UtVJeC6EG0P3J; H_BDCLCKID_SF=tbFJVILKtIvbfP0kKP7MM4LO5eT22-us32Qt2hcH0KLKVROXWlubeqDt-fJZLncGJKviBnrCJfb1MRjveb6WjtP3LqOwXpQy2gTDQl5TtUJaeCnTDMRhqt5W2b6yKMnitIT9-pnobpQrh459XP68bTkA5bjZKxtq3mkjbIOFfD_-MCL4j6L2en-W5gTtatctMCIX3buQt-cr8pcNLTDKj5K8hJ7NqRvZKRKHblv2aU5Wfnk90lO1j4_eKHouK4nZQC5v2nRJKhRi8h5jDh3q3jksD-Rt5t3byTry0hvctn5cShnc5MjrDRLbXU6BK5vPbNcZ0l8K3l02VKO_e6KbDjQQDN-Oq-cJHDv8QRoH5PK_Hn7zenrzLf4pbt-qJfoG2nbe_CTyWJjEhxjo5-vjX5kIKqjnBT5KaK0qbpjj3xPhKfQthRr4K4-kQN3TKRkO5bRi5RoI-UJjDn3oyT3qXp0nMpOly5jtMgOBBJ0yQ4b4OR5JjxonDhOyyG62btt_tbktoD5P; BDRCVFR[CZsEJLjIHxn]=mk3SLVN4HKm; delPer=0; PSINO=5; H_PS_PSSID=; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1555642094,1555642468,1555649103,1555655269; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1555655269; from_lang_often=%5B%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%2C%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%5D; to_lang_often=%5B%7B%22value%22%3A%22en%22%2C%22text%22%3A%22%u82F1%u8BED%22%7D%2C%7B%22value%22%3A%22zh%22%2C%22text%22%3A%22%u4E2D%u6587%22%7D%5D'
        , 'Host': 'fanyi.baidu.com',
        'Origin': 'https: // fanyi.baidu.com',
        'Referer': 'https: // fanyi.baidu.com /?aldtype = 16047',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'
    }
    @staticmethod
    def create_en(text):
        url='https://fanyi.baidu.com/multitransapi'
        data={
            'from':'zh',
            'to':'en',
            'query':text,
        }
        re=requests.post(url,data=data,headers=Baidu.headers)
        ls=re.json()
        return ls['data']['cands']

    @staticmethod
    def create_cn(text):
        url='https://fanyi.baidu.com/v2transapi'
        with open('./fanyi/baidujs.js')as f:
            jsData=f.read()
        inputData=text
        data={
            'query':inputData,
            "sign":execjs.compile(jsData).call("e",inputData),
            "token":"1d8992194dc3afd0fbb021f6e6633b1e",
        }
        try:
            re=requests.post(url,data=data,headers=Baidu.headers,timeout=5)
            re=re.json()
            return re['trans_result']['data'][0] ['dst']
        except:
           return None
    @staticmethod
    def main(text,rest):
        re=Baidu.create_en(text)
        if re:
            for i in re:
                rest.append(Baidu.create_cn(i))

        # print('百度执行')
        # print(rest)

